<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author  Tran Van Thanh
 * @email   thanhtran@vietnambiz.com
 * @date    30.08.2011
 */
 
class Company_model extends MY_Model {
    
    function __construct()
    {
        parent::__construct();
        $this->_table = 'company';
    }
    
    function get_all_company($page = 1)
    {
    	$limit = $this->config->item('company', 'limit');
    	return $this->limit($limit, ($page - 1)*$limit)->order_by('order')->get_all();
    }        

    function delete_company($company_id)
    {     
    	if(is_numeric($company_id)) {
        	$a[] = $company_id;
        	$company_id = $a;
        }
    	$this->db->where_in('id', $company_id)->delete('company');    	
    	$this->delete_many($company_id);    	
    }
    
	function get_max_order()
    {
        return $this->db->select_max('order', 'max_order')->get($this->_table)->row()->max_order;
    }
    
	function change_order($id, $old_order, $new_order)
    {          	
        $max_order = $this->get_max_order();

        if ($new_order < 1 || $new_order > $max_order)
            return false;

        $query = $this->db->get_where('company', array('order' => $new_order));

        if ($query->num_rows() > 0) 
        {
            $data = $query->row_array();            
            $this->db->update('company', array('order' => $old_order), array('id' => $data['id']));
            $this->db->update('company', array('order' => $new_order), array('id' => $id));
        }
        else 
        {
            $this->db->update('company', array('order' => $new_order), array('id' => $id));
        }
    }
}